package com.jinzhi.jzweb.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.BannerDO;
import com.jinzhi.jzweb.domain.FamousTeacherDO;
import com.jinzhi.jzweb.service.BannerService;
import com.jinzhi.sys.domain.RoleDO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/***
 * @author C
 * @Date 2023/12/5
 * @Description: Banner管理
 * @Version 1.0
 */
@Controller
@RequestMapping("/banner")
@Slf4j
@Api(value = "基本信息管理", tags = "基本信息管理")
public class BannerController extends BaseController {

    @Autowired
    private BannerService bannerService;


    /**
     * 分页查询
     *
     * @param model
     * @return
     */
    @ResponseBody
    @GetMapping("/list/{current}/{number}")
    @RequiresPermissions("jzweb:banner:list")
    @ApiOperation(value = "基本信息管理-分页查询", notes = "基本信息管理-分页查询")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result selectPageList(@PathVariable("current") int current, @PathVariable("number") int number, Model model) {
        Page<BannerDO> bannerList = bannerService.selectListPage(current, number);
        model.addAttribute("bannerList", bannerList);

        return Result.ok(bannerList);
    }
    /**
     * 根据id查询banner
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ResponseBody
    @ApiOperation(value = "基本信息管理-根据id查询", notes = "基本信息管理-根据id查询")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result getBanner(@PathVariable("id") Long id) {
        BannerDO bannerDO = bannerService.selectBanner(id);
        return Result.ok(bannerDO);
    }

    /**
     * 新增
     *
     * @param bannerDO
     * @return
     * @throws IOException
     */
    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions("jzweb:banner:add")
    @ApiOperation(value = "基本信息管理-新增", notes = "基本信息管理-新增")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result<String> insertBanner(@RequestBody BannerDO bannerDO) throws IOException {
        bannerService.insertBanner(bannerDO);
        return Result.ok("新增成功");
    }

    /**
     * 修改
     *
     * @param bannerDO
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    @RequiresPermissions("jzweb:banner:edit")
    @ApiOperation(value = "基本信息管理-修改", notes = "基本信息管理-修改")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result<String> editBanner(@RequestBody BannerDO bannerDO) {
        bannerService.updateBanner(bannerDO);
        return Result.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ResponseBody
    @RequiresPermissions("jzweb:banner:remove")
    @ApiOperation(value = "基本信息管理-删除", notes = "基本信息管理-删除")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    public Result<String> deleteBanner(@PathVariable("id") Long id) {
        bannerService.deleteBanner(id);
        return Result.ok("修改成功");
    }


    @ApiOperation(value = "基本信息后台管理页面入口", notes = "基本信息后台管理页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/index")
    String getList(Model model) {
        List<Map> mylist = getRolePageButton("/banner/index");
        String authority = "";
        for (Map map : mylist) {
            authority += map.get("name") + ",";
        }
        authority = authority.length() > 0 ? authority.substring(0, authority.length() - 1) : "";
        model.addAttribute("authorityButtonAdd", authority.contains("新增"));
        model.addAttribute("authorityButtonEdit", authority.contains("修改"));
        model.addAttribute("authorityButtonInquire", authority.contains("查询"));
        model.addAttribute("authorityButtonDelete", authority.contains("删除"));

        model.addAttribute("authority", authority.split(","));

        return "jzweb/banner/index";
    }

    @ApiOperation(value = "基本信息后台管理新增页面入口", notes = "基本信息后台管理新增页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/add")
    String add(Model model) {
        String roleName = getRoleNames();
        String authority = "";
        if (roleName.contains("教师")) {
            authority = "教师";
        } else if (roleName.contains("管理员")) {
            authority = "管理员";
        } else {
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        return "jzweb/banner/add";
    }


    @ApiOperation(value = "基本信息后台管理修改页面入口", notes = "基本信息后台管理修改页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"), @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/update/{id}")
    String update(Model model, @PathVariable("id") Long id) {
        String roleName = getRoleNames();
        BannerDO bannerDO = bannerService.selectBanner(id);
        String authority = "";
        if (roleName.contains("教师")) {
            authority = "教师";
        } else if (roleName.contains("管理员")) {
            authority = "管理员";
        } else {
            authority = "管理员";
        }
        model.addAttribute("authority", authority);
        model.addAttribute("userData", bannerDO);
        return "jzweb/banner/edit";
    }

    /**
     * 查询最新的一条logo信息
     */
    @GetMapping("/getLogo")
    @ResponseBody
    public Result getLogo(){
        BannerDO bannerDO = bannerService.selectLogo();
        if (bannerDO==null){
            return Result.fail("logo信息不存在");
        }else {
            return Result.ok(bannerDO);
        }
    }

}
